import os
import shutil
from pathlib import Path


def clear_directory(directory):
    """清空指定目录下的所有文件和子目录"""
    dir_path = Path(directory)

    if not dir_path.exists():
        print(f"⚠️  目录不存在: {dir_path}")
        return False

    file_count = 0
    dir_count = 0

    for item in dir_path.iterdir():
        try:
            if item.is_file():
                item.unlink()  # 删除文件
                file_count += 1
            elif item.is_dir():
                shutil.rmtree(item)  # 删除目录及其内容
                dir_count += 1
        except Exception as e:
            print(f"  ✗ 删除失败 {item.name}: {e}")
            return False

    print(f"  ✅ 删除 {file_count} 个文件, {dir_count} 个文件夹")
    return True


def main():
    """主函数"""
    # 获取当前脚本所在目录
    current_script_dir = os.path.dirname(os.path.abspath(__file__))

    # 定义要清空的文件夹
    folders_to_clear = [
        Path(current_script_dir) / "工单填写",
        Path(current_script_dir) / "工单PDF",
        Path(current_script_dir) / "工单生成"
    ]

    print("=" * 70)
    print("🗑️  开始清空文件夹...")
    print("=" * 70)
    print()

    # 清空每个文件夹
    for folder in folders_to_clear:
        print(f"🗑️  清空: {folder.name}")
        clear_directory(folder)
        print()

    print("=" * 70)
    print("🎉 清空完成!")
    print("=" * 70)


if __name__ == "__main__":
    main()
